package com.sf.service.impl;

import com.sf.domain.Department;
import com.sf.domain.Employee;
import com.sf.mapper.EmployeeMapper;
import com.sf.qo.EmployeeQueryObject;
import com.sf.qo.QueryObject;
import com.sf.service.IEmployee;
import com.sf.utils.PageResult;
import com.sf.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeImpl implements IEmployee {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public Employee Login(Employee employee) {
        if (StringUtils.isEmpty(employee.getUsername())||StringUtils.isEmpty(employee.getPassword())) {
            throw new RuntimeException("账号密码不能为空");
        }
            Employee employee1 = employeeMapper.Login(employee.getUsername(), employee.getPassword());
        if (employee1 == null) {
            throw new RuntimeException("账号密码错误");
        }
        return employee1;


    }


    @Override
    public PageResult<Employee> query(EmployeeQueryObject qo) {
        int count = employeeMapper.selectForCount(qo);
        if(count == 0) {
            return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(),null
                    , 1);
        }
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), employeeMapper.selectForList(qo)
                ,count);
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Employee employee) {
        employeeMapper.insert(employee);
    }

    @Override
    public void updateByPrimaryKey(Employee employee) {
        if(employee.getId() == null){
            employeeMapper.insert(employee);
        }else{
            employeeMapper.updateByPrimaryKey(employee);
        }
    }

    @Override
    public Employee selectByPrimaryKey(Long id) {
        employeeMapper.selectByPrimaryKey(id);
        return null;
    }

    @Override
    public List<Employee> selectAll() {
        employeeMapper.selectAll();
        return Collections.emptyList();
    }

    @Override
    public Employee get(Long id) {

        employeeMapper.selectByPrimaryKey(id);
        return null;
    }

    @Override
    public void save(Employee employee1) {
        employeeMapper.insert(employee1);
    }

    @Override
    public void update(Employee employee1) {
        employeeMapper.updateByPrimaryKey(employee1);
    }
}
